package com.bwton.msx.ccb.ui;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import com.bwton.msx.ccb.R;
import com.bwton.msx.ccb.utils.PreferenceUtil;
import com.bwton.msx.ccb.widget.dialog.PermissionDialog;
import com.sensetime.ssidmobile.sdk.liveness.STLiveness;


import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.core.content.ContextCompat;
import android.util.Base64;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends FragmentActivity {

    public static int REQUEST_CODE_SILENT = 1 << 1;
    public static int REQUEST_CODE_INTERACTIVE = 1 << 2;
    public static int REQUEST_CODE_COLOR = 1 << 3;
    public static int REQUEST_CODE_PERMISSION = 1 << 4;
    public static int REQUEST_CODE;

    TextView mTxtVersion;
    TextView mTxtMessage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTxtVersion = findViewById(R.id.txt_version);
        findViewById(R.id.btn_interactive).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                REQUEST_CODE = REQUEST_CODE_INTERACTIVE;
                checkPermission();
            }
        });
        findViewById(R.id.img_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, SettingsActivity.class));
            }
        });
        findViewById(R.id.btn_silent).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                REQUEST_CODE = REQUEST_CODE_SILENT;
                checkPermission();
            }
        });
        mTxtVersion.setText(this.getString(R.string.app_version, STLiveness.getVersion()));

        mTxtMessage = findViewById(R.id.message);
        PreferenceUtil.init(this);
    }
    public static String bitmapToBase64(Bitmap bitmap) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        return Base64.encodeToString(byteArray, Base64.DEFAULT);
    }
    @Override
    protected void onResume() {
        super.onResume();
    }

    /**
     * 检查权限
     */
    private void checkPermission() {
        ArrayList<String> permissionArray = new ArrayList<>();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            if (ContextCompat.checkSelfPermission(
                    getApplication(),
                    Manifest.permission.CAMERA
            ) != PackageManager.PERMISSION_GRANTED) {
                permissionArray.add(Manifest.permission.CAMERA);
            }
            if (permissionArray != null && permissionArray.size() != 0) {
                String[] strings = new String[permissionArray.size()];
                permissionArray.toArray(strings);
                requestPermission(strings);
                return;
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {
                permissionArray.add(Manifest.permission.CAMERA);
            }
            if (permissionArray != null && permissionArray.size() != 0) {
                String[] permissions = new String[permissionArray.size()];
                permissionArray.toArray(permissions);
                requestPermission(permissions);
                return;
            }
        }
        dispenseActivity(REQUEST_CODE);
    }

    /**
     * 请求权限
     */
    private void requestPermission(final String[] permissions) {
        new PermissionDialog.Builder()
                .setContext(this)
                .setOnClickListener(new PermissionDialog.OnClickListener() {
                    @Override
                    public void onCancelClick(PermissionDialog dialog) {
                        dialog.cancel();
                    }

                    @Override
                    public void onOkClick(PermissionDialog dialog) {
                        dialog.cancel();
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                            ActivityCompat.requestPermissions(
                                    MainActivity.this, permissions,
                                    REQUEST_CODE_PERMISSION
                            );
                        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            requestPermissions(permissions,
                                    REQUEST_CODE_PERMISSION
                            );
                        }
                    }
                })
                .build().show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        if (requestCode == REQUEST_CODE_PERMISSION) {
            dealPermissionResult(requestCode, grantResults);
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void dealPermissionResult(int requestCode, int[] grantResults) {
        for (int i = 0; i < grantResults.length; i++) {
            if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                //permission denied
                Toast.makeText(this, R.string.permission_denied, Toast.LENGTH_LONG).show();
                return;
            }
        }
        dispenseActivity(REQUEST_CODE);
    }

    private void dispenseActivity(int id) {
        if (id == REQUEST_CODE_SILENT) {
            SilentActivity.start(this);
        } else if (id == REQUEST_CODE_INTERACTIVE) {
            InteractiveActivity.start(this);
        }
    }

}